// chrome text highlighter
// by mike urbach
//
// if you're reading this, feel free to figure out how to 
// make this a nasty extension for doing homework in google docs

var highlightTxt = '';
var highlightKey;

document.addEventListener('mouseup',highlight);
document.addEventListener('keydown', getKey);

function getKey(e) 
{
	highlightKey = e.keyCode;
}

function highlight(e)
{
	if(e.ctrlKey || highlightKey == 91 || highlightKey == 93)	
	{
		highlightTxt = window.getSelection().getRangeAt(0);
		span = document.createElement('span');
		span.style.cssText = "background-color: yellow";
		try {
		    highlightTxt.surroundContents(span);	
		} catch(BAD_BOUNDARYPOINTS_ERR) {
		    // should deal with invalid or interesting
		    // HTML fragments more delicately
		    try {
			highlightTxt.surroundContents(highlightTxt.commonAncestor);
		    } catch(NOT_FOUND_ERR) {
			// silence is golden
		    }
		}
	}
}